package com.akl.ssmspringboot.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * @author guoxy
 * @date 2019/1/9 11:18
 * @description
 */
public class HelloScheduler {

    public static void main(String[] args) {

        try {
            //创建scheduler
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            //定义一个Trigger
            Trigger trigger = newTrigger().withIdentity("trigger1", "group1") //定义name/group
                    .startNow()//一旦加入scheduler，立即生效
                    .withSchedule(simpleSchedule() //使用SimpleTrigger
                            .withIntervalInSeconds(2) //每隔2秒执行一次
                            .repeatForever()) //一直执行，奔腾到老不停歇
                    .build();

            //定义一个JobDetail，将实例与HelloQuartz绑定
            JobDetail job = newJob(HelloQuartz.class)
                    .withIdentity("jobName1", "group1") //定义name/group
                    .usingJobData("name", "quartz") //定义属性
                    .build();

            System.out.println("JobDetail's name: "+job.getKey().getName());
            System.out.println("JobDetail's group: "+job.getKey().getGroup());
            System.out.println("JobDetail's class: "+job.getKey().getClass());

            //加入这个调度
            scheduler.scheduleJob(job, trigger);

            //启动之
            scheduler.start();

            //运行一段时间(10s)后关闭
            Thread.sleep(10000);
            scheduler.shutdown(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
